<?php
    require_once('./include/SellerClass.php');
    require_once('./include/AdminClass.php');
    require_once('./adminSettings.php');
    
    $SellClass = new SellerClass();
    $adminClass = new AdminClass();
    $userID = $_SESSION['user']['id'];

    
    if (!empty($_GET['action']))
        {
            if (!empty($_GET['go']))
            {
                $name = $_GET['name'];
                $desc = $_GET['desc'];
                $picture = $_GET['picture'];


                switch ($_GET['action'])
                {
                    case 'new' :{
                                    $activeSession = $_SESSION['user']['session'];
                                    $result = $SellClass->addNewItem($name,$desc,$userID,$picture,$activeSession);
                                    break;
                                }

                    case 'upd' :{
                                    $picture = ($picture) ? $picture : "images/question_mark.png";
                                    $id = $_GET['id'];
                                    $result = $SellClass->updateItem($id,$name,$desc, $picture);
                                    break;
                                }

                    case 'del' :{
                                    $id = $_GET['id'];
                                    $result = $SellClass->deleteItem($id);
                                    break;
                                }
                }
                if ($result==true)
                {
                    echo "Done! ! !";
                    
                    echo "<script>setTimeout(\"document.location.href='index.php?tabID=adminMain&subID=items'\",3000);</script>";
                    return;
                }
                else
                    echo "error - $result";
            }
           else
           {
           require_once('sellerItemForm.php');
           return;
           }
        }
    
    echo "
    <div >
    <h2>Admin defined items:</h2>
    <input type='button' class='newButton' onClick=\"document.location.href='index.php?tabID=sellerMain&subID=items&action=new'\" value='Add a new item'>";
        echo "
               <table class='auctionListTable' >
                    <tr class='auctionListCat'>
                            <td>Name</td>
                            <td>Description</td>
                            <td class='imageTag'>Picture</td>
                            <td class='delTag'>Delete</td>
                            <td class='updTag'>Update</td>
                    </tr>";
        
        $items = $adminClass->getAdminItemList($_SESSION['user']['session']);
        $num = mysql_num_rows($items);
        $i=0;
        if ($num==0)
            echo "<br>No items found";
        else 
        {
            while($row = mysql_fetch_array($items))
            {
                echo "
                <tr class='adminAuctionLines'>
                        <td>".$row['name']."</td>
                        <td>".substr($row['desc'], 0, 30)."...</td>                                   
                        <td><img class='thumbnail' src='".$row['picture']."' alt=\"picture\"/> </td>
                        <td><a href=\"./index.php?tabID=adminMain&subID=items&action=del&id=". $row['prodID'] ."\">
                            <img src='images/delete.png' alt='Del' class='deleteImg'/>
                        </td>
                        <td><a href=\"./index.php?tabID=adminMain&subID=items&action=upd&id=". $row['prodID'] ."\">
                            <img src='images/update.png' alt='Upd' class='updateImg' />
                        </td>
                </tr>";
            }
        }
        echo   "</table>
        </div>";
    
        
 
?>

